UserAccount   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 55
c 0
b 0
f 0
dl 0
loc 97
rs 10
wmc 13

5 Functions

Rating   Name   Duplication   Size   Complexity  
A initAvatarCropper 0 18 2
A constructor 0 6 1
A addAvatarTypeListener 0 29 3
A initFileUpload 0 20 5
A addAvatarSubmitButtonListener 0 6 2
1
import Cropper from 'cropperjs'
2
3
export class UserAccount {
4
    constructor() {
5
        this.addAvatarTypeListener()
6
        this.addAvatarSubmitButtonListener()
7
        this.initFileUpload()
8
        this.initAvatarCropper()
9
    }
10
11
    /**
12
     * Add a listener on the avatar type radio buttons and ajust display according to the user choice.
13
     */
14
    addAvatarTypeListener() {
15
        let container = $('#change_avatar')
16
17
        $('input[name="avatar_type"]', container).on('change', (event) => {
18
            let element = event.currentTarget
19
20
            $('.info span', container).hide()
21
            $('.profile-image img, .profile-image div').hide()
22
            $('.image-upload').hide()
23
24
            switch($(element).val()) {
25
                case 'initials':
26
                        $('.info span.avatar-initials', container).show()
27
                        $('.profile-image .initials').show()
28
                    break
29
30
                case 'gravatar':
31
                        $('.info span.avatar-gravatar', container).show()
32
                        $('.profile-image .gravatar').show()
33
                    break
34
35
                case 'image':
36
                        $('.info span.avatar-image', container).show()
37
                        $('.profile-image .image').show()
38
                        $('.image-upload').show()
39
                    break
40
            }
41
        })
42
    }
43
44
    /**
45
     * Add a listner on the submit button
46
     */
47
    addAvatarSubmitButtonListener() {
48
        $('#avatar-submit').on('click', (event) => {
0 ignored issues
show
Unused Code introduced by
The parameter event is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
49
            $("#avatar-input").val(this.canvasDataUrl)
50
            $('#change_avatar form').submit()
51
        })
52
    }
53
54
    /**
55
     * Init file upload.
56
     */
57
    initFileUpload() {
58
        $('#avatar-file').on('change', (event) => {
59
            let files = event.currentTarget.files
60
61
            if (files && files.length > 0) {
62
                let file = files[0]
63
64
                let reader = new FileReader()
0 ignored issues
show
Bug introduced by
The variable FileReader seems to be never declared. If this is a global, consider adding a /** global: FileReader */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
65
                reader.onload = (e) => {
0 ignored issues
show
Unused Code introduced by
The parameter e is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
66
                    this.cropper.replace(reader.result)
67
                }
68
                reader.readAsDataURL(file)
69
            }
70
        })
71
72
        $('#upload-link').on('click', event => {
73
            event.preventDefault()
74
            $('#avatar-file').click()
75
        })
76
    }
77
78
    /**
79
     * Init cropper for adjust the avatar image.
80
     */
81
    initAvatarCropper() {
82
        const image = document.getElementById('avatar-image-preview');
83
        this.cropper = new Cropper(image, {
84
            aspectRatio: 1,
85
            autoCropArea: 1,
86
            minContainerWidth: 250,
87
            minContainerHeight: 250,
88
            zoomable: false,
89
            crop: (event) => {
0 ignored issues
show
Unused Code introduced by
The parameter event is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
90
                let canvas = this.cropper.getCroppedCanvas()
91
92
                // Convert image into data string
93
                this.canvasDataUrl = canvas.toDataURL()
94
95
                $("img.image").prop('src', this.canvasDataUrl)
96
            },
97
        })
98
    }
99
}